// pages/match/matchorder.js
const app=getApp();

const log =require('../../log.js');
const util=require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_id:null,
    match_id:null,
    match_type_id:null,
    matchtype:null,
    select:0,
    index:1,
    number:'',
    code:'',
    toView:'ty0'

  },
 
  orderPay:function(order_id){
    var that=this;
    var open=wx.getStorageSync('open');
    wx.request({
      url:app.globalData.domain+'match/match/Pay',    
      data: {order_id: order_id, open_id: open.id, openid: open.mini_openid },
      success: function (res) {
       console.log(res.data);
        //console.log('调起支付');
        var code=res.data.code;
        if(code==200){
        var data=res.data.data;
          wx.requestPayment({
              'timeStamp': data.timeStamp,
              'nonceStr': data.nonceStr,
              'package': data.package,
              'signType': 'MD5',
              'paySign': data.paySign,
              'success': function (res) {
               // console.log('success');
                wx.showToast({
                  title: '支付成功',
                  icon: 'success',
                  duration: 3000
                });
                setTimeout(function() {
                    //要延时执行的代码
                    wx.navigateTo({
                      url: '/pages/mine/orderid=1',
                    })
                  }, 1000) //延迟时间
                //that.waitPayShow();
              },
              'fail': function (res) {
                //console.log(res);
              },
              'complete': function (res) {
               // console.log('complete');
              }
            });
        }else{
  
        }
    
      },
      fail: function (res) {
        //console.log("fail")
      }
    }); 
  
  },
  formSubmit:function(e){
    console.log(e);
    const that=this;
    var open=wx.getStorageSync('open'); 
    console.log(open);
    var unionid=open.unionid;
    let datas = this.data;
    var match=datas.match;
    var order_money=match.type.sale_money;
    var user = datas.user;
    console.log('user=>')
    console.log(user);
    var select = datas.select;
    var code = datas.code;
    if(select==0){

    }else{
        wx.request({
         // url: 'https://lypb.com/lupao/enrolls.php',
          url: app.globalData.domain+'match/match/enroll',
          header: {
            'Content-Type': 'application/x-www-form-urlencoded',
          },
          data: { 
              match_id: datas.match_id, 
              match_type: match.type.match_type,
              match_type_id:datas.match_type_id,
              member_id: open.member_id,
              order_money:order_money,
              type:datas.match.types,
               from:1, 
               open_id:open.id,
               openid:open.mini_openid, 
               name: user.name,
               phone: user.phone, 
                idcard_type: user.card_type, 
                idcard: user.idcard, 
                coat: user.coat,          
                province:user.province,
                city:user.city,
                area:user.area,
                address:user.address,
                link_name:user.link_name,                 
                link_phone:user.link_phone,
                sex:user.sex,
                blood:user.blood,
                number:datas.number,
                ids:datas.ids,
                numberMoney:datas.numberMoney,
                code:code
            },
          success:function(res){
            var data=res.data;
            var url=data.url;
            var index=data.index;
            var msg=data.msg;
              console.log(data)
            if (res.data.code==200){
              var order_id=res.data.order_id;
              that.showModels(msg,url,index)
            }else{
              that.showModels(msg,url,index)
              }
          },
          fail:function(res){

          }
        })

      
      }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     console.log('---------options-------');
      console.log(options);
      console.log('---------options-------');
    console.log(app.globalData);
 
    var match_id = options.match_id;
    var match_type= options.match_type;
    var that = this;
    var open=wx.getStorageSync('open');
    console.log(open);
    var date=util.formatTime(new Date());
    log.info("Time:"+date+",match:order,options:"+JSON.stringify(options));
    log.info("Time:"+date+",match:order,open:"+JSON.stringify(open));
    if(open==undefined){
      wx.navigateTo({
        url: '../index/index',
      })
    }
    if(open==""){
      wx.navigateTo({
        url: '../index/index',
      })
    }
    this.data.match_id = match_id;
    this.data.match_type=match_type;
    this.data.match_type_id=match_type;
    this.setData({
      domain:app.globalData.domain,
      width:app.globalData.windowWidth,
      height:app.globalData.windowHeight,
      statusBarHeight:app.globalData.statusBarHeight,
      navHeight:app.globalData.navHeight,
      marginTop:app.globalData.navTitleTop,
   
    })
   // //console.log(that.data.match_type)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    


  },
  goBack:function(){
    wx.navigateBack();
  },
  goto(e){
      console.log(e);
      var url=e.currentTarget.dataset.url;
      var index=e.currentTarget.dataset.index;
      if(url!=undefined){
        if(index=='#'){
            wx.navigateTo({
              url: url,
            })
        }else{
            wx.reLaunch({
              url: url,
            })
        }
      }

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getIndex();
  },
  getIndex(){
      let that=this;
      let data=this.data;
    var open=wx.getStorageSync('open');
    wx.request({      
        url: app.globalData.domain+'match/match/preOrder',
        data: { match_id: data.match_id,match_type:data.match_type,open_id:open.id},
        success: function (res){
            let datas=res.data;
          console.log(res.data);
        // that.data.user_list = res.data['user'];
          that.setData({
            match: datas.match,
            NumberType:datas.numberType
       
          });
          }
      })
  },
  getNumberType(sex){
      var match_type=this.data.match_type;
      var sex=this.data.user.sex;
      console.log(match_type+'+++'+sex);
 
      let that=this;
      let datas=this.data;
      wx.request({
        url: app.globalData.domain+'match/number/getNumbers',
        data: { match_type:datas.match_type,sex:sex,ids:1},
        success:res=>{
            console.log(res);
            var data=res.data;
            var list=data.list;
            var indexs=1;
            that.setData({
                numberList:data.list,
                indexs:1,
               // numList:list[indexs].list
            })
        }
      })
      
  },
  selectNumberType(e){
      console.log(e)
    var index=e.currentTarget.dataset.index;
    var ids = e.currentTarget.dataset.ids;
    var match_type=this.data.match_type;
    let that = this;
    let datas=this.data;
      var sex=this.data.user.sex;
    var list=this.data.numberList;
    var indexs=this.data.indexs;
    var NumberType = datas.NumberType;
    console.log(NumberType);
    for (let i = 0; i < NumberType.length; ++i) {
        console.log(NumberType[i].id);
        var id = NumberType[i].id;
        var scrollTop = ids*90;
        console.log(scrollTop);
        if ("ty"+id === this.data.toView) {
            //scrollTop = i*90;
        console.log(scrollTop);
          this.setData({
            toView: "ty"+id,
           
          })
          break
        }
        this.setData({
           
            scrollTop:scrollTop
          })
      }
    console.log(indexs);
    console.log(index);
    if(index!=indexs){
        wx.request({
            url: app.globalData.domain+'match/number/getNumbers',
            data: { match_type:datas.match_type,sex:sex,ids:index},
            success:res=>{
                console.log(res);
                var data=res.data;
                var list=data.list;
                var indexs=1;
                that.setData({
                    numberList:data.list,
                //indexs:1,
                    //numList:list[indexs].list
                })
            }
          })
        this.setData({
            indexs:index,
           // numList:list[index].list
        })
    }
  },
  selectNumber(e){
      console.log(e);
      
      var data=e.currentTarget.dataset;
      var id=data.id;
      var number=data.number;
      var money=data.money;
      this.setData({
          ids:id,
          number:number,
          numberMoney:money
      });
  },
  submitOrder(){
      let that=this;
      let data=this.data;
   
      var select=data.select;
      if(select==0){
        this.showModels('请选择参赛者','#','#')
      }else{
        var match=data.match;
        var match_name=match.title;
        var name=data.user.name;
        var match_type=match.type.match_type;
        var number=data.number;
        var selectNumber=match.number;
        if(selectNumber==3 && number==''){
          
            this.showModels('请选择参赛号','#','#');
         
        }else{

      var msg='比赛名称：'+match_name+'\r\n比赛类型：'+match_type+' \r\n参赛人姓名：'+name;
   
      wx.showModal({
        title: '报名信息',
        content: msg,
        success(res) {
           if (res.confirm) {
             console.log('用户点击确定');
             that.formSubmit();
           } else if (res.cancel) {
             console.log('用户点击取消')
           }
        }
     })
      }
    }
  },
  inputCode(e){
    this.setData({
      code:e.detail.value
    })
  },
  showModels(msg,url,index){
    wx.showModal({
      title: '重要提示',
      content: msg,
      success(res) {
         if (res.confirm) {
           console.log('用户点击确定');
           if(url!='#'){
              if(index=='index'){
                wx.reLaunch({
                  url: url,
                })
              }else{
                wx.navigateTo({
          
                  url: url,
                })
              }
           }
          // that.formSubmit();
         } else if (res.cancel) {
           console.log('用户点击取消')
         }
      }
   })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.getIndex();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})